package com.cz.nongyetongv5.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.activity.AssistPatrolActivity;
import com.cz.nongyetongv5.bean.City;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhangliang on 2017/6/26.
 */

public class AreaPopPatrolFragment extends Fragment {

    private RecyclerView recyclerView;
    private ListAdapter adapter;
    private ArrayList<City> cities;
    private View view;


    public static AreaPopPatrolFragment newInstance(List<City> cities) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("city", (Serializable) cities);
        AreaPopPatrolFragment fragment = new AreaPopPatrolFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_area, null);
        recyclerView = view.findViewById(R.id.recyclerView);
        cities = new ArrayList<>();
        cities.addAll((List<City>) getArguments().getSerializable("city"));
        initAdapter();
        return view;
    }


    private void initAdapter() {
        adapter = new ListAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int ps) {
                if (cities.get(ps).getCode().length()==8){
                    for (City city : cities) {
                        city.setChecked(false);
                    }
                    cities.get(ps).setChecked(true);
                    adapter.notifyDataSetChanged();
                }else {
                    for (City city : cities) {
                        city.setChecked(false);
                    }
                    cities.get(ps).setChecked(true);
                    ((AssistPatrolActivity) getActivity()).setUpdate(cities.get(ps).getName(), cities.get(ps).getCode());
                }
            }
        });
    }


    public String getSelectName() {
        String name = "";
        for (City city : cities) {
            if (city.isChecked()) {
                name = city.getName();
                return name;
            }
        }
        return name;
    }

    public City getSelectCity() {
        for (City city : cities) {
            if (city.isChecked()) {
//                System.out.println(city.toString());
                return city;
            }
        }
        return null;
    }

    public class ListAdapter extends BaseQuickAdapter<City, BaseViewHolder> {
        public ListAdapter() {
            super(R.layout.item_area, cities);
        }

        @Override
        protected void convert(BaseViewHolder helper, City item) {
            helper.setText(R.id.tv_name, item.getName());
            //判断是否选中
            if (item.isChecked()) {
                helper.setTextColor(R.id.tv_name, Color.parseColor("#11B57C"));
            } else {
                helper.setTextColor(R.id.tv_name, Color.parseColor("#656565"));
            }
        }
    }



}